# leetcode 104 二叉树最大深度

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

# 后序遍历实现
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        def get_height(node):
            # 求节点node的最大高度
            if node is None:
                return 0
            left_height = get_height(node.left)
            right_height = get_height(node.right)
            height = 1 + max(left_height, right_height)
            return height
        return get_height(root) # 根节点的最大高度，就是二叉树的最大深度


# 层序遍历实现
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        queue = []
        result = 0
        if not root:
            return result
        queue.append(root)
        while queue:
            size = len(queue)
            while size:
                node = queue.pop(0)
                size -= 1
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            result += 1
        return result
